import storage from 'good-storage'

const SEARCH_INDEX_KEY = 'index_search';//二手房搜索历史记录
const SEARCH_VIDEO_KEY = 'video_search';//视频搜索历史记录
const SEARCH_NEWS_KEY = 'news_search';//新闻搜索历史记录
const SEARCH_PREMISE_KEY = 'premise_search';//新楼盘历史搜索
const CITY_KEY = 'city_key';//城市
const LOOK_TYPE = 'look_type';//预约看房类型 1 二手房 2 楼盘 3 租房
const MENU_KEY = 'menu_key';//底部菜单
const SEARCH_TYPE = 'search_type';//搜索类别
const LATITUDE = 'latitude';//地理位置 经度
const LONGITUDE = 'longitude';//地理位置 纬度
const ROUTER_PARAMS = 'router_params';//存储路由参数，即ID
const SEARCH_MAX_LENGTH = 15;//最大个数

// 二手房搜索
// compare:findindex传入的是function,所以不能直接传val
function indexInsertArray(arr, val, compare, maxLen) {
  const index = arr.findIndex(compare);
  if (index === 0) {
    return
  }
  if (index > 0) {
    arr.splice(index, 1)
  }
  arr.unshift(val) ;// 插入到数组最前
  if (maxLen && arr.length > maxLen) {
    arr.pop() // 删除末位元素
  }
}

// 存储二手房搜索历史
export function saveIndexSearch(query) {
  let searches = storage.get(SEARCH_INDEX_KEY, []);
  indexInsertArray(searches, query, (item) => {
    return item === query
  }, SEARCH_MAX_LENGTH);
  storage.set(SEARCH_INDEX_KEY, searches);
  return searches
}
// 加载本地缓存的搜索历史
export function loadIndexSearch() {
  return storage.get(SEARCH_INDEX_KEY, []);
}


//视频搜索
function videoInsertArray(arr, val, compare, maxLen) {
  const index = arr.findIndex(compare);
  if (index === 0) {
    return
  }
  if (index > 0) {
    arr.splice(index, 1)
  }
  arr.unshift(val) ;// 插入到数组最前
  if (maxLen && arr.length > maxLen) {
    arr.pop() // 删除末位元素
  }
}
// 存储视频搜索历史
export function saveVideoSearch(query) {
  let searches = storage.get(SEARCH_VIDEO_KEY, []);
  videoInsertArray(searches, query, (item) => {
    return item === query
  }, SEARCH_MAX_LENGTH);
  storage.set(SEARCH_VIDEO_KEY, searches);
  return searches
}
// 加载视频本地缓存的搜索历史
export function loadVideoSearch() {
  return storage.get(SEARCH_VIDEO_KEY, []);
}
//资讯搜索
function newsInsertArray(arr, val, compare, maxLen) {
  const index = arr.findIndex(compare);
  if (index === 0) {
    return
  }
  if (index > 0) {
    arr.splice(index, 1)
  }
  arr.unshift(val) ;// 插入到数组最前
  if (maxLen && arr.length > maxLen) {
    arr.pop() // 删除末位元素
  }
}
//存储资讯搜索
export function saveNewsSearch(query) {
  let searches = storage.get(SEARCH_NEWS_KEY, []);
  newsInsertArray(searches, query, (item) => {
    return item === query
  }, SEARCH_MAX_LENGTH);
  storage.set(SEARCH_NEWS_KEY, searches);
  return searches
}
//加载
export function loadNewsSearch() {
  return storage.get(SEARCH_NEWS_KEY, []);
}
//楼盘存储
function premiseInsertArray(arr, val, compare, maxLen) {
  const index = arr.findIndex(compare);
  if (index === 0) {
    return
  }
  if (index > 0) {
    arr.splice(index, 1)
  }
  arr.unshift(val) ;// 插入到数组最前
  if (maxLen && arr.length > maxLen) {
    arr.pop() // 删除末位元素
  }
}
//存储新楼盘搜索
export function savepremiseSearch(query) {
  let searches = storage.get(SEARCH_PREMISE_KEY, []);
  premiseInsertArray(searches, query, (item) => {
    return item === query
  }, SEARCH_MAX_LENGTH);
  storage.set(SEARCH_PREMISE_KEY, searches);
  return searches
}
//加载
export function loadPremiseSearch() {
  return storage.get(SEARCH_PREMISE_KEY, []);
}
//存储底部菜单栏
export function setMenuList(query) {
  return storage.set(MENU_KEY,query)
}
//获取底部导航栏
export function getMenuList(query) {
  return storage.get(MENU_KEY,query);
}
//城市选择(存储)
export function setCityName(query) {
  return storage.set(CITY_KEY,query);
}
// 调用选择的城市
export function getCityName(query) {
  return storage.get(CITY_KEY,query);
}

//储存搜索类别
export function setSearchType(query) {
  return storage.set(SEARCH_TYPE,query);
}
// 调用搜索类别
export function getSearchType(query) {
  return storage.get(SEARCH_TYPE,query);
}
//存储用户地理坐标/IP坐标 经度
export function setLatitude(query) {
  return storage.set(LATITUDE,query)
}
//纬度
export function setLongitude(query) {
  return storage.set(LONGITUDE,query)
}
//调用用户地理坐标/IP坐标 经度
export function getLatitude(query) {
  return storage.get(LATITUDE,query)
}
//纬度
export function getLongitude(query) {
  return storage.get(LONGITUDE,query)
}
//储存看房类型
export function setLookType(query) {
  return storage.set(LOOK_TYPE,query)
}
//调用看房类型
export function getLookType(query) {
  return storage.get(LOOK_TYPE,query)
}
//存储路由参数
export function setRouterParams(query) {
  return storage.set(ROUTER_PARAMS,query)
}
//调用路由参数
export function getRouterParams(query) {
  return storage.get(ROUTER_PARAMS,query);
}
